<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
<!--      <el-form-item label="客户名称" prop="name">-->
<!--        <el-input-->
<!--          v-model="queryParams.name"-->
<!--          placeholder="请输入客户名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="类型" prop="type">-->
<!--        <el-select-->
<!--          v-model="queryParams.type"-->
<!--          placeholder="请选择类型"-->
<!--          clearable-->
<!--          class="!w-240px"-->
<!--        >-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="客户ID" prop="customerId">-->
<!--        <el-input-->
<!--          v-model="queryParams.customerId"-->
<!--          placeholder="请输入客户ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="客户名称" prop="customerName">-->
<!--        <el-input-->
<!--          v-model="queryParams.customerName"-->
<!--          placeholder="请输入客户名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="合同ID" prop="contractId">-->
<!--        <el-input-->
<!--          v-model="queryParams.contractId"-->
<!--          placeholder="请输入合同ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="合同名称" prop="contractName">-->
<!--        <el-input-->
<!--          v-model="queryParams.contractName"-->
<!--          placeholder="请输入合同名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="合同编号" prop="contractCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.contractCode"-->
<!--          placeholder="请输入合同编号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="期数" prop="periods">-->
<!--        <el-input-->
<!--          v-model="queryParams.periods"-->
<!--          placeholder="请输入期数"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="服务团队ID" prop="serviceTeamId">-->
<!--        <el-input-->
<!--          v-model="queryParams.serviceTeamId"-->
<!--          placeholder="请输入服务团队ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="服务团队" prop="serviceTeamName">-->
<!--        <el-input-->
<!--          v-model="queryParams.serviceTeamName"-->
<!--          placeholder="请输入服务团队"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="部门" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="层级顺序" prop="layerOrder">-->
<!--        <el-input-->
<!--          v-model="queryParams.layerOrder"-->
<!--          placeholder="请输入层级顺序"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="付款金额" prop="payMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.payMoney"-->
<!--          placeholder="请输入付款金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="已匹配金额" prop="macthMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.macthMoney"-->
<!--          placeholder="请输入已匹配金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="付款日期" prop="payDate">-->
<!--        <el-date-picker-->
<!--          v-model="queryParams.payDate"-->
<!--          value-format="YYYY-MM-DD HH:mm:ss"-->
<!--          type="daterange"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
<!--          class="!w-220px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="共同执行人" prop="coexecutor">-->
<!--        <el-input-->
<!--          v-model="queryParams.coexecutor"-->
<!--          placeholder="请输入共同执行人"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备注" prop="remarks">-->
<!--        <el-input-->
<!--          v-model="queryParams.remarks"-->
<!--          placeholder="请输入备注"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="银行" prop="bankName">-->
<!--        <el-input-->
<!--          v-model="queryParams.bankName"-->
<!--          placeholder="请输入银行"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="版本号" prop="versionNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.versionNum"-->
<!--          placeholder="请输入版本号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="状态" prop="status">-->
<!--        <el-select-->
<!--          v-model="queryParams.status"-->
<!--          placeholder="请选择状态"-->
<!--          clearable-->
<!--          class="!w-240px"-->
<!--        >-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="录入人ID" prop="recorderId">-->
<!--        <el-input-->
<!--          v-model="queryParams.recorderId"-->
<!--          placeholder="请输入录入人ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="经手人" prop="recorderName">-->
<!--        <el-input-->
<!--          v-model="queryParams.recorderName"-->
<!--          placeholder="请输入经手人"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="录入日期" prop="recordDate">-->
<!--        <el-date-picker-->
<!--          v-model="queryParams.recordDate"-->
<!--          value-format="YYYY-MM-DD HH:mm:ss"-->
<!--          type="daterange"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
<!--          class="!w-220px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="发票" prop="invoiceCodes">-->
<!--        <el-input-->
<!--          v-model="queryParams.invoiceCodes"-->
<!--          placeholder="请输入发票"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="实际付款日期" prop="payDateImp">-->
<!--        <el-input-->
<!--          v-model="queryParams.payDateImp"-->
<!--          placeholder="请输入实际付款日期"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="订单号" prop="orderNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.orderNo"-->
<!--          placeholder="请输入订单号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="收款日期" prop="effectDateStr">
        <el-date-picker
          v-model="queryParams.effectDateStr"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
<!--      <el-form-item label="应收金额" prop="totalMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.totalMoney"-->
<!--          placeholder="请输入应收金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="实收金额" prop="receivedMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.receivedMoney"-->
<!--          placeholder="请输入实收金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="本次匹配金额" prop="thisMacthMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.thisMacthMoney"-->
<!--          placeholder="请输入本次匹配金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="需要匹配" prop="needPay">-->
<!--        <el-input-->
<!--          v-model="queryParams.needPay"-->
<!--          placeholder="请输入需要匹配"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="匹配信息" prop="macthInfo">-->
<!--        <el-input-->
<!--          v-model="queryParams.macthInfo"-->
<!--          placeholder="请输入匹配信息"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="对应服务期" prop="serviceDate">-->
<!--        <el-date-picker-->
<!--          v-model="queryParams.serviceDate"-->
<!--          value-format="YYYY-MM-DD HH:mm:ss"-->
<!--          type="daterange"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
<!--          class="!w-220px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['pay:biz-payment-management:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="warning"
          plain
          @click="handleImport"
          v-hasPermi="['pay:biz-payment-management:export']"
        >
          <Icon icon="ep:upload" /> 导入
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['pay:biz-payment-management:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
<!--        <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            :disabled="isEmpty(checkedIds)"-->
<!--            @click="handleDeleteBatch"-->
<!--            v-hasPermi="['pay:biz-payment-management:delete']"-->
<!--        >-->
<!--          <Icon icon="ep:delete" class="mr-5px" /> 批量删除-->
<!--        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
        :preserve-expanded-content="preserveExpanded"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column type="expand">
        <template #default="props">
          <div style="width:90%;margin-left: 120px">
            <el-table :data="props.row.payment" :border="false" :show-overflow-tooltip="true">
              <el-table-column label="应收款日期" prop="effectDate"  align="center" :formatter="dateFormatter2"
                               width="120px"/>
              <el-table-column label="实际收款日期" prop="payDateImp"  align="center" :formatter="dateFormatter2"
                               width="120px"/>
<!--              <el-table-column label="状态" prop="status"  align="center"/>-->
              <el-table-column label="类型" prop="type" align="center" width="200" >
                <template v-slot="scope">
                  <dict-tag :type="DICT_TYPE.BIZ_CONTRACT_ARCHIVE_CONTRACT_TYPE" :value="scope.row.type" />
                </template>
              </el-table-column>
              <el-table-column label="合同编号" prop="contractCode" width="120" align="center" />
              <el-table-column label="合同名称" prop="contractName" width="200"  align="center" show-overflow-tooltip/>
              <el-table-column label="期数" prop="periods"  align="center" />
              <el-table-column label="应收金额" prop="totalMoney"  align="center"/>
              <el-table-column label="已收金额" prop="payMoney" align="center" />
              <el-table-column label="未收金额" align="center" prop="totalMoney"  >
                <template #default="scope">
                  {{parseInt(scope.row.totalMoney ? scope.row.totalMoney:0) - parseInt(sumPaymentsBq(props.row.payment,scope.row.periods) ?sumPaymentsBq(props.row.payment,scope.row.periods):0)}}
                </template>
              </el-table-column>
<!--              <el-table-column>-->
<!--                {{parseInt(props.row.totalMoney ? props.row.totalMoney:0) - parseInt(scope.row.payMoney ?scope.row.payMoney:0)}}-->
<!--              </el-table-column>-->
              <el-table-column label="对应服务期" prop="serviceTerm" width="250" align="center"/>
<!--                {{formatDate(props.row.serviceDate,'YYYY-MM-DD')}}-->
<!--                至-->
<!--                {{formatDate(props.row.serviceDateEnd,'YYYY-MM-DD')}}-->
<!--              </el-table-column>-->
              <el-table-column label="发票" prop="invoiceCodes"  align="center"/>
              <el-table-column label="经手人" prop="recorderName"  align="center"/>
              <el-table-column label="操作" fixed="right" align="center" width="120">
                <template #default="scope">
                  <el-button
                    link
                    type="primary"
                    @click="openForm('update', scope.row.id)"
                    v-hasPermi="['pay:biz-payment-management:update']"
                  >
                    编辑
                  </el-button>
                  <el-button
                    link
                    type="danger"
                    @click="handleDelete(scope.row.id)"
                    v-hasPermi="['biz:contract-archive-receive-record:delete']"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="合同编号" align="center" prop="contractArchiveInfo.contractCode" width="150" />
      <el-table-column label="合同名称" align="center" prop="contractArchiveInfo.contractName"  width="180" />
      <el-table-column label="客户名称" align="center" prop="contractArchiveInfo.customerName"  width="120" />
<!--      <el-table-column label="客户名称" align="center" prop="name" />-->
      <el-table-column label="付款日期" align="center" prop="payDate"  width="100" >
        <template #default="scope">
          {{formatDate(scope.row.payDate,'YYYY-MM-DD')}}
        </template>
      </el-table-column>
      <el-table-column label="应收金额" align="center" prop="contractArchiveInfo.contractPriceNumber" width="100" />
      <el-table-column label="已收金额" align="center" prop="payMoney" width="100" >
        <template #default="scope">
          {{ sumPayments(scope.row.payment) }}
        </template>
      </el-table-column>
      <el-table-column label="剩余金额" align="center" prop="totalMoney" width="100"  >
        <template #default="scope">
          {{parseInt(scope.row.contractArchiveInfo.contractPriceNumber ? scope.row.contractArchiveInfo.contractPriceNumber:0) - parseInt(sumPayments(scope.row.payment) ?sumPayments(scope.row.payment):0)}}
        </template>
      </el-table-column>
      <el-table-column label="期数" align="center" prop="contractArchiveInfo.instalmentCount" />
      <el-table-column label="需要匹配" align="center" prop="needPay" />
      <el-table-column label="匹配信息" align="center" prop="macthInfo" width="120" />
      <el-table-column label="收款内容" align="center" prop="content" width="120"  />
      <el-table-column label="经手人" align="center" prop="recorderName" width="100"  />
      <el-table-column label="银行" align="center" prop="bankName" width="120" />
      <el-table-column label="类型" align="center" prop="type" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BIZ_PAY_SERVICE_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="发票" align="center" prop="invoiceCodes" />
      <el-table-column label="服务团队" align="center" prop="contractArchiveInfo.serviceUnitName" />
      <el-table-column
        label="录入日期"
        align="center"
        prop="recordDate"
        :formatter="dateFormatter2"
        width="150px"
      />
      <el-table-column label="收款日期" align="center" prop="effectDate"  width="150px" >
        <template #default="scope">
          {{formatDate(scope.row.effectDate,'YYYY-MM-DD')}}
        </template>
      </el-table-column>
<!--      <el-table-column label="客户ID" align="center" prop="customerId" />-->
<!--      <el-table-column label="合同ID" align="center" prop="contractId" />-->
<!--      <el-table-column label="服务团队ID" align="center" prop="serviceTeamId" />-->
<!--      <el-table-column label="部门" align="center" prop="deptId" />-->
<!--      <el-table-column label="层级顺序" align="center" prop="layerOrder" />-->
<!--      <el-table-column label="共同执行人" align="center" prop="coexecutor" />-->
<!--      <el-table-column label="版本号" align="center" prop="versionNum" />-->
<!--      <el-table-column label="状态" align="center" prop="status" />-->
<!--      <el-table-column label="录入人ID" align="center" prop="recorderId" />-->
<!--      <el-table-column label="实际付款日期" align="center" prop="payDateImp" />-->
<!--      <el-table-column label="订单号" align="center" prop="orderNo" />-->
<!--      <el-table-column label="应收金额" align="center" prop="totalMoney" />-->
<!--      <el-table-column label="实收金额" align="center" prop="receivedMoney" />-->
<!--      <el-table-column label="本次匹配金额" align="center" prop="thisMacthMoney" />-->
<!--      <el-table-column label="对应服务期" align="center" prop="serviceDate" />-->
<!--      <el-table-column label="操作" fixed="right" align="center" min-width="120px">-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--            link-->
<!--            type="primary"-->
<!--            @click="openForm('update', scope.row.id)"-->
<!--            v-hasPermi="['pay:biz-payment-management:update']"-->
<!--          >-->
<!--            编辑-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            link-->
<!--            type="danger"-->
<!--            @click="handleDelete(scope.row.id)"-->
<!--            v-hasPermi="['pay:biz-payment-management:delete']"-->
<!--          >-->
<!--            删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <BizPaymentManagementForm ref="formRef" @success="getList" />
  <!-- 导入对话框 -->
  <ImportForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter,dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { BizPaymentManagementApi, BizPaymentManagement } from '@/api/biz/payment'
import BizPaymentManagementForm from './BizPaymentManagementForm.vue'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
const props = defineProps<{
  contractCode: string
}>()
const preserveExpanded = ref(false);
const open = async (id?: string) => {
  // dialogVisible.value = true
  // dialogTitle.value = '拓展客户'
  handleQuery()
}
/** 导入 */
import ImportForm from './ImportForm.vue'
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}
/** 收款管理 列表 */
defineOptions({ name: 'BizPaymentManagement' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
import { formatDate } from '@/utils/formatTime'

const loading = ref(true) // 列表的加载中
const list = ref<BizPaymentManagement[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const route = useRoute()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  // name: undefined,
  // type: undefined,
  // customerId: undefined,
  // customerName: undefined,
  // contractId: undefined,
  // contractName: undefined,
  contractCode: props.contractCode as any,
  // periods: undefined,
  // serviceTeamId: undefined,
  // serviceTeamName: undefined,
  // deptId: undefined,
  // layerOrder: undefined,
  // payMoney: undefined,
  // macthMoney: undefined,
  // payDate: [],
  // coexecutor: undefined,
  // content: undefined,
  // remarks: undefined,
  // bankName: undefined,
  // versionNum: undefined,
  // status: undefined,
  // recorderId: undefined,
  // recorderName: undefined,
  // recordDate: [],
  // invoiceCodes: undefined,
  // payDateImp: undefined,
  // orderNo: undefined,
  effectDateStr: [],
  // totalMoney: undefined,
  // receivedMoney: undefined,
  // thisMacthMoney: undefined,
  // needPay: undefined,
  // macthInfo: undefined,
  // serviceDate: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

// 返回对象数组中付款金额的总和
const sumPayments = (arr) => arr.reduce((sum, item) => sum + (item.payMoney || 0), 0);
const sumPaymentsBq = (arr,qs) => {
  let newArr = arr.filter(a => a.periods == qs);
  return newArr.reduce((sum, item) => sum + (item.payMoney || 0), 0);
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await BizPaymentManagementApi.getBizPaymentManagementPage(queryParams)
    list.value = data.records
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BizPaymentManagementApi.deleteBizPaymentManagement(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除收款管理 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await BizPaymentManagementApi.deleteBizPaymentManagementList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: BizPaymentManagement[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await BizPaymentManagementApi.exportBizPaymentManagement(queryParams)
    download.excel(data, '收款管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  const contractCode = route.query.id
  if (contractCode) { // 只有存在 id 时才刷新
    queryParams.contractCode = contractCode
    getList()
  } else {
    getList()
  }
})
</script>
